# -*- coding: utf-8 -*- 
# @Time : 2021/3/26/026 10:30 
# @Author : BuildRT
# @File : question_parser.py 

class QuestionPaser:

    '''构建实体节点'''
    def build_entitydict(self, args):
        entity_dict = {}
        for arg, types in args.items():
            for type in types:
                if type not in entity_dict:
                    entity_dict[type] = [arg]
                else:
                    entity_dict[type].append(arg)

        return entity_dict

    '''解析主函数'''
    def parser_main(self, res_classify):
        args = res_classify['args']
        entity_dict = self.build_entitydict(args)
        question_types = res_classify['question_types']
        sqls = []
        for question_type in question_types:
            sql_ = {}
            sql_['question_type'] = question_type
            sql = []

            if question_type == 'groups_foodCategory':
                sql = self.sql_transfer(question_type, entity_dict.get('groups'))

            elif question_type == 'foodCategory_groups':
                sql = self.sql_transfer(question_type, entity_dict.get('foodCategory'))

            elif question_type == 'foodCategory_food':
                sql = self.sql_transfer(question_type, entity_dict.get('foodCategory'))

            elif question_type == 'food_foodCategory':
                sql = self.sql_transfer(question_type, entity_dict.get('food'))

            elif question_type == 'food_energy':
                sql = self.sql_transfer(question_type, entity_dict.get('food'))

            elif question_type == 'food_vitamin':
                sql = self.sql_transfer(question_type, entity_dict.get('food'))

            elif question_type == 'food_mineral':
                sql = self.sql_transfer(question_type, entity_dict.get('food'))

            elif question_type == 'food_fatty_acid':
                sql = self.sql_transfer(question_type, entity_dict.get('food'))

            if sql:
                sql_['sql'] = sql

                sqls.append(sql_)

        return sqls

    '''针对不同的问题，分开进行处理'''
    def sql_transfer(self, question_type, entities):
        if not entities:
            return []

        # 查询语句
        sql = []
        # 查询某个大类中有哪些小类
        # MATCH (n:`食物分类`)-[r:`包含`]->(m:`食品种类`) where n.group_name="谷类及制品" RETURN n.group_name,m.food_category_name
        if question_type == 'groups_foodCategory':
            sql = ["MATCH (n:`食物分类`)-[r:`包含`]->(m:`食品种类`) where n.group_name = '{0}' RETURN n.group_name,m.food_category_name".format(i) for i in entities]

        # 查询某个小类是哪个大类
        # MATCH (n:`食物分类`)-[r:`包含`]->(m:`食品种类`) where m.food_category_name="大麦" RETURN n.group_name,m.food_category_name
        elif question_type == 'foodCategory_groups':
            sql = ["MATCH (n:`食物分类`)-[r:`包含`]->(m:`食品种类`) where m.food_category_name = '{0}' RETURN n.group_name,m.food_category_name".format(i) for i in entities]

        # 查询某个小类中有哪些食物
        # MATCH (n:`食品种类`)-[:`分类`]->(m:`食品`) where n.food_category_name = "小麦"  RETURN n.food_category_name,m.food_name
        elif question_type == 'foodCategory_food':
            sql = ["MATCH (n:`食品种类`)-[:`分类`]->(m:`食品`) where n.food_category_name = '{0}'  RETURN n.food_category_name,m.food_name".format(i) for i in entities]

        # 询问某个食物是属于哪个小类的
        # MATCH (n:`食品种类`)-[:`分类`]->(m:`食品`) where m.food_name = "春卷"  RETURN n.food_category_name,m.food_name
        elif question_type == 'food_foodCategory':
            sql = ["MATCH (n:`食品种类`)-[:`分类`]->(m:`食品`) where m.food_name = '{0}'  RETURN n.food_category_name,m.food_name".format(i) for i in entities]

        # 询问某个食物的能量与相关成分
        # MATCH (n:`食品`)-[:`详情`]->(m:`能量与相关成分`) where n.food_name = "油条" RETURN n.food_name,m.nutrient_name,m.content
        elif question_type == 'food_energy':
            sql = ["MATCH (n:`食品`)-[:`详情`]->(m:`能量与相关成分`) where n.food_name = '{0}' RETURN n.food_name,m.nutrient_name,m.content".format(i) for i in entities]

        # 询问某个食物的维生素
        # MATCH (n:`食品`)-[:`详情`]->(m:`维生素`) where n.food_name = "橙" RETURN n.food_name,m.nutrient_name,m.content
        elif question_type == 'food_vitamin':
            sql = ["MATCH (n:`食品`)-[:`详情`]->(m:`维生素`) where n.food_name = '{0}' RETURN n.food_name,m.nutrient_name,m.content".format(i) for i in entities]

        # 询问某个食物的矿物质
        # MATCH (n:`食品`)-[:`详情`]->(m:`矿物质`) where n.food_name = "橙" RETURN n.food_name,m.nutrient_name,m.content
        elif question_type == 'food_mineral':
            sql = ["MATCH (n:`食品`)-[:`详情`]->(m:`矿物质`) where n.food_name = '{0}' RETURN n.food_name,m.nutrient_name,m.content".format(i) for i in entities]

        # 询问某个食物的脂肪酸
        # MATCH (n:`食品`)-[:`详情`]->(m:`脂肪酸`) where n.food_name = "猪蹄" RETURN n.food_name,m.nutrient_name,m.content
        elif question_type == 'food_fatty_acid':
            sql = ["MATCH (n:`食品`)-[:`详情`]->(m:`脂肪酸`) where n.food_name = '{0}' RETURN n.food_name,m.nutrient_name,m.content".format(i) for i in entities]

        return sql